package com.aoxin.controller.client;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aoxin.model.course.SlfOrderCourse;
import com.aoxin.service.inter.course.systemCourse.OrderCourseService;
import com.aoxin.service.inter.course.systemCourse.SyllabusService;
import com.aoxin.util.AjaxRes;
import com.aoxin.util.Const;
import com.aoxin.util.ThreadLocalUtil;
import com.aoxin.util.enumerate.OperateType;
import com.aoxin.util.enumerate.UserType;
import com.aoxin.util.log.LogInfo;

/**
 * 客服端预约
 * 
 * @author Melody
 * @date 2018年4月11日 下午2:34:10
 */
@Controller
@ResponseBody
@RequestMapping("/web/order")
public class OrderController {

	@Autowired
	private SyllabusService syllabusService;

	@Autowired
	private OrderCourseService orderCourseService;

	@RequestMapping("/getSyllabusDate")
	public AjaxRes getSyllabusDate(String ymdDate, AjaxRes ajaxRes) {
		try {
			List<String> list = this.syllabusService.getSyllabusDate(null, ymdDate);
			ajaxRes.setSucceed(list, Const.DATA_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 添加预约
	 * 
	 * @param orderCourse
	 * @param ajaxRes
	 * @return
	 */
	@RequestMapping("/insertOrderCourse")
	public AjaxRes insertOrderCourse(SlfOrderCourse orderCourse, AjaxRes ajaxRes) {
		try {
			orderCourse.setChildrenId(ThreadLocalUtil.getChildren().getChildrenId());
			this.orderCourseService.insertOrderCourse(orderCourse);
			ajaxRes.setSucceedMsg(Const.SAVE_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 删除预约
	 * 
	 * @param orderCourseIds
	 * @param ajaxRes
	 * @return
	 */
	@RequestMapping("/deleteOrderCourses")
	public AjaxRes deleteOrderCourses(@Param("orderCourseIds") List<Integer> orderCourseIds, AjaxRes ajaxRes) {
		try {
			this.orderCourseService.deleteOrderCourses(orderCourseIds);
			ajaxRes.setSucceedMsg(Const.DEL_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 修改预约
	 * 
	 * @param orderCourse
	 * @param ajaxRes
	 * @return
	 */
	@RequestMapping("/updateOrderCourse")
	public AjaxRes updateOrderCourse(SlfOrderCourse orderCourse, AjaxRes ajaxRes) {
		try {
			this.orderCourseService.updateOrderCourse(orderCourse);
			ajaxRes.setSucceedMsg(Const.UPDATE_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 通过ID获取预约信息
	 * 
	 * @param orderCourseId
	 * @return
	 */
	@RequestMapping("/getOrderCourse")
	public AjaxRes getOrderCourse(Integer orderCourseId, AjaxRes ajaxRes) {
		try {
			SlfOrderCourse slfOrderCourse = this.orderCourseService.getOrderCourse(orderCourseId);
			ajaxRes.setSucceed(slfOrderCourse, Const.DATA_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 查询预约
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping("/getOrderCourseList")
	public AjaxRes getOrderCourseList(@Param("map") Map<String, Object> map, AjaxRes ajaxRes) {
		try {
			List<SlfOrderCourse> slfOrderCourseList = this.orderCourseService.getOrderCourseList(map);
			ajaxRes.setSucceed(slfOrderCourseList, Const.DATA_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 取消预约
	 * 
	 * @param ajaxRes
	 * @return
	 */
	@RequestMapping("/cancelOrder")
	@LogInfo(content = "取消预约", operateType = OperateType.UPDATE, userType = UserType.USER)
	public AjaxRes cancelOrder(@RequestParam("syllabusId") Integer syllabusId, AjaxRes ajaxRes) {
		try {
			Integer childrenId = ThreadLocalUtil.getChildren().getChildrenId();
			this.orderCourseService.cancelOrder(syllabusId, childrenId);
			ajaxRes.setSucceedMsg(Const.UPDATE_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}
}
